//
//  YLVerificationCodeLoginController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/16.
//  Copyright © 2017年 she1323. All rights reserved.
//  验证码登录界面

#import "YLVerificationCodeLoginController.h"
#import "YLLoginUserViewModel.h"
#import "YLGlobeHeader.h"
#import <YYText.h>
#import <Masonry.h>
#import "YLRegisterViewController.h"
#import "YLVerificationCodeViewModel.h"
#import "WHProgressHUD.h"
#import "WHNetworkTool.h"
#import "YLMineViewModel.h"
#import "YLSwitchRootController.h"
#import "YLPushViewModel.h"

@interface YLVerificationCodeLoginController ()
@property (weak, nonatomic) IBOutlet UIButton *userLoginButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
@property (nonatomic, strong) YLLoginUserViewModel *viewModel;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UITextField *verificationCodeTextField;
@property (weak, nonatomic) IBOutlet UIButton *verificationCodeButton;
@property (weak, nonatomic) IBOutlet UIView *phoneNumberLine;

@property (weak, nonatomic) IBOutlet UIView *verififcationLine;
@property (nonatomic, strong) YLVerificationCodeViewModel *codeModel;
@end

@implementation YLVerificationCodeLoginController




- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //   [[self.navigationController.navigationBar.subviews objectAtIndex:0]setAlpha:0] ;
    
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    //   [[self.navigationController.navigationBar.subviews objectAtIndex:0]setAlpha:0] ;
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBackground"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setUpUI];
    
    [self bindModel];
    
    [self loginEvent];
}

- (YLVerificationCodeViewModel *)codeModel
{
    if (!_codeModel) {
        _codeModel = [[YLVerificationCodeViewModel alloc] init];
        _codeModel.urlStr = @"Account/PGetLoginVerifyCode";
    }
    return _codeModel;
}

- (YLLoginUserViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLLoginUserViewModel alloc] init];
    }
    return _viewModel;
}

- (void)setUpUI
{
    __weak typeof(self) weakSelf = self;
    
    _verififcationLine.backgroundColor = _phoneNumberLine.backgroundColor = YLLineColor;
    self.title = @"验证码登录";
    
    YYLabel *titleLabel = [YYLabel new];
    [self.view addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf.view);
        make.top.mas_equalTo(weakSelf.userLoginButton.mas_bottom).mas_offset(10);
    }];
    
    titleLabel.attributedText = [self.viewModel attributedStringWithString:@" 密码登录  |  没有账号, 立即注册"];
    
    [[_phoneNumberTextField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.phoneNumberLine.backgroundColor = YLMainColor;
        weakSelf.verififcationLine.backgroundColor = YLLineColor;
    }];
    
    [[_verificationCodeTextField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.phoneNumberLine.backgroundColor = YLLineColor;
        weakSelf.verififcationLine.backgroundColor = YLMainColor;
    }];
    
    [_phoneNumberTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 11) {
            weakSelf.phoneNumberTextField.text = [weakSelf.phoneNumberTextField.text substringToIndex:11];
        }
    }];
    
    [_verificationCodeTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 6) {
            weakSelf.verificationCodeTextField.text = [weakSelf.verificationCodeTextField.text substringToIndex:6];
        }
    }];

}



- (void)bindModel
{
    RAC(self.viewModel, account) = _phoneNumberTextField.rac_textSignal;
    RAC(self.viewModel, password) = _verificationCodeTextField.rac_textSignal;
    RAC(_loginButton, enabled) = self.viewModel.loginEnableSiganl;
    
}

- (void)loginEvent
{
    __weak typeof(self) weakSelf = self;
    
    
    [[self.verificationCodeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf.codeModel.verificationCommand execute:weakSelf.phoneNumberTextField.text];
    }];
   
    [self.codeModel.verificationCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        [weakSelf.verificationCodeButton setTitle:x forState:UIControlStateNormal];
    }];
    
    
    [[self.codeModel.verificationCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
        if ([x boolValue]) {
            weakSelf.verificationCodeButton.enabled = NO;
        } else {
            weakSelf.verificationCodeButton.enabled = YES;
        };
    }];
    
    
    [self.viewModel.otherLoginSubject subscribeNext:^(id  _Nullable x) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    
    [self.viewModel.registerSubject subscribeNext:^(id  _Nullable x) {
        YLRegisterViewController *registerController = [[YLRegisterViewController alloc] init];
        [weakSelf.navigationController pushViewController:registerController animated:YES];
    }];
    
    
//    [[self.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//        [weakSelf.viewModel.loginCommand execute:nil];
//    }];
//
//    [self.viewModel.loginCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
//    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)login:(UIButton *)sender {
    __weak typeof(self) weakSelf = self;
    [[self.viewModel.otherLoginCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        [weakSelf setUp];
    }];
}

- (void)setUp
{
    __weak typeof(self) weakSelf = self;
    [[[[YLMineViewModel alloc] init].myInfoCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        [WHProgressHUD dismiss];
        weakSelf.view.userInteractionEnabled = YES;
        [YLSwitchRootController switchRootController];
        [YLSwitchRootController switchMineOrRealName];
        [YLPushViewModel setUpMyPushInfo];
    }];
}


@end
